/*--------------------------------1--------------------------------------------*/
//  response 错误统一处理
axios.interceptors.response.use(
    res => {
        if(res.data.status==='101')
        {
            alert('请先登录');
            return Promise.reject(res);
        }
        return res;
    },
    err => {
        return Promise.reject(err);
    }
);
new Vue({
    el: '#app',
    data() {
        return {
            showCart: false,
            loading:false,
            products:{},
            cart:{}
        };
    },
    methods: {
        getProducts(){
            let thisApp = this;
            axios.get(apiGetProducts).then(function (response) {
                let rawProducts = response.data;
                let tranProducts = {};
                for(let i in rawProducts)
                {
                    tranProducts[rawProducts[i].id]=rawProducts[i];
                }
                /* 使用过渡性变量[tranProducts]进行中间处理，然后一次性赋值给app变量[thisApp.products] */
                thisApp.products = tranProducts;
            }).catch(function (error) {
                alert('Error! Could not reach the API. ');
                console.log(error);
            });
        },
        getCart(){
            let cookieCart = this.$cookies.get('cart');
            if(cookieCart !== null)
                this.cart = JSON.parse(this.$cookies.get('cart'));
        },
        createOrder(){
            if(JSON.stringify(this.cart)==='{}'){
                alert('请先选购商品');
                return;
            }

            let thisApp = this;
            this.loading = true;
            axios.post(apiCreateOrder,this.cart).then(function (response) {
                console.log(response.data);
                thisApp.clearCart();
                thisApp.loading = false;
            }).catch(function (error) {
                alert('Error! Could not reach the API. ');
                console.log(error);
            });
        },

        addCart(product_id){
            if(this.cart[product_id]){
                this.cart[product_id].quantity++;
            }else
            {
                // this.cart[product_id].product_price = this.products[product_id].product_price;
                let tranCartProduct = {};
                /*注意js只支持一维空属性赋值，不支持二维空索引赋值，否则会报属性未定义错误，深维空属性赋值请使用中间对象进行过渡 */
                tranCartProduct.id = this.products[product_id].id;
                tranCartProduct.product_name = this.products[product_id].product_name;
                tranCartProduct.product_price = this.products[product_id].product_price;
                tranCartProduct.quantity = 1;
                this.$set(this.cart,product_id,tranCartProduct)
            }
            this.$cookies.set('cart', JSON.stringify(this.cart));
        },
        subCart(product_id){
            if(this.cart[product_id].quantity>0){
                this.cart[product_id].quantity--;
                if(this.cart[product_id].quantity === 0)
                {
                    delete this.cart[product_id];
                }
                this.$cookies.set('cart', JSON.stringify(this.cart));
            }
        },
        clearCart(){
            this.cart={};
            this.$cookies.remove('cart');
        }
    },
    computed:{
        total: function () {
            let total = {price:0,quantity:0};
            for(let i in this.cart)
            {
                total.price += this.cart[i].product_price * this.cart[i].quantity;
                total.quantity += this.cart[i].quantity;
            }
            return total;
        },
    },
    mounted () {
        this.getProducts();
        this.getCart();
    }
});
